#include "Environment.h"

using namespace std; 

Environment::Environment(){
  _blockSize = B;
  _access = 0;
  _result = 0; 
  _method = 0;
  _nbThread = 2;
  _thread = NULL; 
  _filename = (char*)malloc(sizeof(char)*sizeof(NAME1));
  _filename = NAME1; 
}

Environment::Environment(Environment * env, char * filename){
  _blockSize = env->_blockSize;
  _access = env->_access;
  _result = env->_result; 
  _method = env->_method;
  _nbThread = env->_nbThread;
  _thread = env->_thread; 
  _filename = (char*)malloc(sizeof(char)*sizeof(NAME1));
  _filename = filename;
}

Environment::~Environment(){
  free(_thread);
  free(_filename);
}

int Environment::getBlockSize(){
  return _blockSize; 
}

int Environment::getAccess(){
  return _access; 
}

int Environment::getMethod(){
  return _method; 
}

pthread_t * Environment::getThread(){
  return _thread; 
}

void Environment::setFilename(char * filename){
  strcpy(_filename, filename);
}

void Environment::setBlockSize(int blockSize){
  _blockSize = blockSize; 
}

void Environment::setBlockSize(string value){
  if(!value.compare("B")){
    setBlockSize(B);
  }else if(!value.compare("KB")){
    setBlockSize(KB);
  } else if(!value.compare("MB")){
    setBlockSize(MB);
  } else if(!value.compare("GB")){
    setBlockSize(GB);
  }else{
    setBlockSize(B);
  }
}

void Environment::setAccess(int access){
  _access = access; 
}

void Environment::setAccess(string value){
  if(!value.compare("sequential")){
    setAccess(1);
  } else if (!value.compare("random")){
    setAccess(2); 
  } else {
    setAccess(0);
  }
}


void Environment::setMethod(int method){
  _method = method; 
}

void Environment::setMethod(string value){
  if(!value.compare("read")){
    setMethod(1);
  } else if (!value.compare("write")){
    setMethod(2); 
  } else {
    setMethod(0);
  }
}

void Environment::setThread(pthread_t* thread){
  _thread = thread;
}

void Environment::setThread(string value){
  if(!value.compare("1")){
    _nbThread = 1; 
  } 
  _thread =(pthread_t*) malloc(sizeof(pthread_t)*_nbThread); 
}

void Environment::toPrint(){

  cout << "BlockSize is " << _blockSize << endl; 
  cout << "Access is " << _access << endl; 
  cout << "Method is " << _method << endl; 
  cout << "Number of thread is "  << _nbThread << endl; 
  cout << "File nme : " << _filename << endl; 
  cout << "Result is " << _result << endl ; 
}

int Environment::getNbThread(){
  return _nbThread;
}
